package com.moyun.zbmy.youxian.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.moyun.zbmy.youxian.R;
import com.moyun.zbmy.youxian.beans.MediaCircleBean;
import com.moyun.zbmy.youxian.beans.ServiceDataBean;
import com.moyun.zbmy.youxian.ui.utils.GlideCircleTransform;
import com.moyun.zbmy.youxian.ui.utils.GlideUtil;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;

import java.util.List;

public class MediaCircleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<MediaCircleBean> dataList;
    private Context ctx;
    private OnMediaItemClickListener mediaItemClickListener;


    static class MediaViewHolder extends RecyclerView.ViewHolder {
        LinearLayout layout;
        CustomFontTextView mediaName;
        ImageView mediaIcon;

        public MediaViewHolder(View view) {
            super(view);
            layout = (LinearLayout) view.findViewById(R.id.item_layout);
            mediaName = (CustomFontTextView) view.findViewById(R.id.media_name);
            mediaIcon = (ImageView) view.findViewById(R.id.media_icon);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        ctx = parent.getContext();
        View v = v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_media_circle, parent, false);
        holder = new MediaViewHolder(v);
        return holder;
    }

    public void setData(List<MediaCircleBean> list) {
        if (null != list) {
            dataList = list;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final MediaCircleBean info = dataList.get(position);
        final MediaViewHolder viewHolder = (MediaViewHolder) holder;
        GlideUtil.getGlid(ctx, info.getIamge()).diskCacheStrategy(DiskCacheStrategy.NONE).transform(new GlideCircleTransform(ctx)).into(viewHolder.mediaIcon);
        viewHolder.mediaName.setText(info.getName());
        viewHolder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mediaItemClickListener) {
                    mediaItemClickListener.onMediaItemClick(info);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (ListUtils.isListValued(dataList)) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    public void setMediaItemClickListener(OnMediaItemClickListener mediaItemClickListener) {
        this.mediaItemClickListener = mediaItemClickListener;
    }

    public interface OnMediaItemClickListener {
        void onMediaItemClick(MediaCircleBean item);
    }
}
